import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Req,
  Put,
  UseInterceptors,
  UploadedFile,
  Query
} from '@nestjs/common'
import { FileInterceptor } from '@nestjs/platform-express/multer'
import * as xlsx from 'xlsx'
import { StaffService } from './staff.service'
import { CreateStaffDto } from './dto/create-staff.dto'
import { UpdateStaffDto } from './dto/update-staff.dto'
import { NoAuth } from 'src/role/decorators/no-auth.decorator'
import { ApiOperation, ApiTags, ApiBody, ApiParam } from '@nestjs/swagger'
import { FResponse } from 'src/utils/reponse'

@NoAuth()
@ApiTags('员工')
@Controller('staff')
export class StaffController {
  constructor(private readonly staffService: StaffService) {}

  @Post()
  @ApiOperation({ summary: '创建员工' })
  @ApiBody({ type: CreateStaffDto, description: '员工信息' })
  async create(@Body() createStaffDto: CreateStaffDto) {
    return FResponse.success(await this.staffService.create(createStaffDto))
  }

  @Post('upload')
  @ApiOperation({ summary: '批量创建员工' })
  @UseInterceptors(FileInterceptor('file'))
  async batchCreate(@UploadedFile() file) {
    const fileData = file.buffer
    const workbook = xlsx.read(fileData, { type: 'buffer' })
    const worksheet = workbook.Sheets[workbook.SheetNames[0]]
    const data: CreateStaffDto[] = xlsx.utils.sheet_to_json(worksheet)
    await Promise.all(data.map(item => this.staffService.create(item)))
    return FResponse.success('批量创建成功')
  }

  @Post('all')
  @ApiOperation({ summary: '获取所有员工' })
  @ApiBody({
    description: '员工信息'
  })
  async findAll(
    @Body()
    body: {
      page: number
      pageSize: number
      isDelete?: boolean
      departmentId?: string
      jobId?: string
      sex?: string
      name?: string
      workStateId?: string
    }
  ) {
    return FResponse.success(
      await this.staffService.findAll({
        page: +body.page,
        pageSize: +body.pageSize,
        isDelete: body.isDelete,
        departmentId: +body.departmentId,
        jobId: +body.jobId,
        sex: body.sex,
        name: body.name,
        workStateId: +body.workStateId
      })
    )
  }

  // 获取真全部员工
  @Get('all/:id')
  @ApiOperation({ summary: '通过jobId获取正常员工' })
  async findAllOption(@Param('id') id: number, @Query('type') type?: boolean) {
    return FResponse.success(await this.staffService.findAllOption(+id, type))
  }

  @Get('info')
  @ApiOperation({ summary: '获取用户信息', description: '获取用户信息接口' })
  async info(@Req() req) {
    return FResponse.success(await this.staffService.findOne(req.user.id))
  }

  @Get(':id')
  @ApiOperation({ summary: '获取单个员工' })
  @ApiParam({ name: 'id', description: '员工id' })
  async findOne(@Param('id') id: string) {
    return FResponse.success(await this.staffService.findOne(+id))
  }

  @Patch(':id')
  @ApiOperation({ summary: '更新员工' })
  @ApiParam({ name: 'id', description: '员工id' })
  @ApiBody({ type: UpdateStaffDto, description: '员工信息' })
  async update(
    @Param('id') id: string,
    @Body() updateStaffDto: UpdateStaffDto
  ) {
    return FResponse.success(
      await this.staffService.update(+id, updateStaffDto)
    )
  }

  // 批量删除
  @Delete()
  @ApiOperation({ summary: '批量删除员工' })
  @ApiBody({ type: [Number], description: '员工ID数组' })
  async batchRemove(@Body() ids: number[]) {
    return FResponse.success(await this.staffService.remove(ids))
  }

  // 批量恢复
  @Put('restore')
  @ApiOperation({ summary: '批量恢复员工' })
  @ApiBody({ type: [Number], description: '员工ID数组' })
  async batchRestore(@Body() ids: number[]) {
    return FResponse.success(await this.staffService.restore(ids))
  }
}
